package com.aisile.sellergoods.service.impl;

import com.aisile.entity.PageResult;
import com.aisile.mapper.TbSellerMapper;
import com.aisile.pojo.TbSeller;
import com.aisile.pojo.TbSellerExample;
import com.aisile.sellergoods.service.SellerService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @标题 SellerServiceImpl
 * @作者 Mr.chen
 * @创建日期 2019/1/2 15:25
 * @版本号 1.0
 **/
@Service
public class SellerServiceImpl implements SellerService {
    @Autowired
    TbSellerMapper tbSellerMapper;

    @Override
    public void save(TbSeller tbSeller) {
        tbSeller.setStatus("0");
        tbSeller.setCreateTime(new Date());


        tbSellerMapper.insert(tbSeller);
    }

    @Override
    public PageResult findPage(int page, int rows, TbSeller tbSeller) {
        PageHelper.startPage(page,rows);
        String name = tbSeller.getName();
        String nickName = tbSeller.getNickName();
        String status = tbSeller.getStatus();
        TbSellerExample tbSellerExample = new TbSellerExample();
        if(!StringUtils.isEmpty(name)){
            tbSellerExample.createCriteria().andNameLike("%"+name+"%");
        }
        if(!StringUtils.isEmpty(nickName)){
            tbSellerExample.createCriteria().andNickNameLike("%"+nickName+"%");
        }
        if(!StringUtils.isEmpty(status)){
            tbSellerExample.createCriteria().andStatusEqualTo(status);
        }
        Page<TbSeller> pageInfo = (Page<TbSeller>)tbSellerMapper.selectByExample(tbSellerExample);
        return new PageResult(pageInfo.getTotal(),pageInfo.getResult());
    }

    @Override
    public TbSeller findById(String sellerId) {
        return tbSellerMapper.selectByPrimaryKey(sellerId);
    }

    @Override
    public void updateStatus(String sellerId, String status) {
        TbSeller tbSeller = tbSellerMapper.selectByPrimaryKey(sellerId);
        tbSeller.setStatus(status);
        tbSellerMapper.updateByPrimaryKey(tbSeller);
    }
}
